package com.topscomm.base;

import com.alibaba.fastjson.JSONObject;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.BasicSystemConst;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.event.CboTableDataChangeEvent;
import com.topscomm.mis.IMisDao;
import com.topscomm.mis.IMisService;
import com.topscomm.mis.MisEntity;
import com.topscomm.mis.MisService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.ITapDao;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.TapService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Classname MarketBasicService
 * @Date 2022/3/8
 * @Created by jiangtao
 */
public class MarketBasicService<T extends MisEntity, TDao extends IMisDao> extends MisService<T, TDao>
		implements IMisService<T, TDao> {
	@Autowired
	private AmqpTemplate rabbitTemplate;
	private static ArrayList<String> bidList = new ArrayList<>();
	private static ArrayList<String> contractList = new ArrayList<>();
	private static ArrayList<String> chanceList = new ArrayList<>();
	static {
		bidList.add("LTC_BID_APPLY");
		contractList.add("LtcContractApply");
		contractList.add("LtcOutContractApply");
		contractList.add("LtcPurchaseOrderApply");
		chanceList.add("LtcChanceBackbone");
	}
	@Override
	protected void afterInsert(BasicEntity entity) throws ServiceException {
		super.afterInsert(entity);
		JSONObject json;
		String tableName = dao.getTableName();
		Map<String,Object> result = new HashMap<>();
		result.put("operateType",BasicSystemConst.BasicDataOperateEnum.Insert);
		result.put("tablename",tableName);
		if(bidList.indexOf(tableName)!=-1) {
			result.put("sourceid",entity.getId());
			result.put("modifiedon",entity.getModifiedon());
			String chance_code = ConvertUtil.convertToString(entity.getValue("chance_code"));
			if(StringUtil.isEmpty(chance_code)) {
				return;
			}
			result.put("chancecode",chance_code);
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcBidQueue",json.toJSONString());
		} else if (contractList.indexOf(tableName) != -1){
			result = this.getContractSendMap(entity.convertToMap(),tableName,BasicSystemConst.BasicDataOperateEnum.Insert);
			if(result.size() == 0) {
				return;
			}
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcContractQueue",json.toJSONString());
		} else if (chanceList.indexOf(tableName)!=-1) {
			result.put("sourceid",entity.getId());
			result.put("modifiedon",entity.getModifiedon());
			long leadid = ConvertUtil.convertToLong(entity.getValue("leadid"));
			if(leadid<=0) {
				return;
			}
			result.put("leadid",leadid);
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcChanceQueue",json.toJSONString());
		}
	}
	@Override
	protected void updateAfter(Map<String, Object> map) throws ServiceException {
		super.updateAfter(map);
		JSONObject json;
		String tableName = dao.getTableName();
		Map<String, Object> result = new HashMap<>();
		result.put("operateType", BasicSystemConst.BasicDataOperateEnum.Update);
		result.put("tablename", tableName);
		if(bidList.indexOf(tableName)!=-1) {
			String chance_code = ConvertUtil.convertToString(map.get("chance_code"));
			if(StringUtil.isEmpty(chance_code)) {
				return;
			}
			result.put("chancecode",chance_code);
			result.put("sourceid",map.get("id"));
			result.put("modifiedon",ConvertUtil.convertToDate(map.get("modifiedon")));
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcBidQueue",json.toJSONString());
		} else if(contractList.indexOf(tableName) != -1) {
			result = this.getContractSendMap(map,tableName,BasicSystemConst.BasicDataOperateEnum.Update);
			if(result.size() == 0) {
				return;
			}
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcContractQueue",json.toJSONString());
		}  else if (chanceList.indexOf(tableName)!=-1) {
			result.put("sourceid",map.get("id"));
			result.put("modifiedon",ConvertUtil.convertToDate(map.get("modifiedon")));
			long leadid = ConvertUtil.convertToLong(map.get("leadid"));
			if(leadid<=0) {
				return;
			}
			result.put("leadid",leadid);
			json = new JSONObject(result);
			this.rabbitTemplate.convertAndSend("ltcChanceQueue",json.toJSONString());
		}
	}

	/***
	 * @description: 获取机会点信息
	 * @param map
	 * @param tableName
	 * @param operateType
	 * @return
	 * @author: jiangtao1
	 * @date: 2022/3/8 18:53
	 * @modify:
	 */
	private Map getContractSendMap(Map<String, Object> map,String tableName,String operateType){
		Map<String, Object> result = new HashMap<>();
		result.put("operateType", BasicSystemConst.BasicDataOperateEnum.Update);
		result.put("tablename", tableName);
		result.put("sourceid",map.get("id"));
		result.put("modifiedon",ConvertUtil.convertToDate(map.get("modifiedon")));
		String contractsource = "";
		String bidcode = "";
		String chancecode = "";
		if("LtcPurchaseOrderApply".equals(tableName)) {
			contractsource = ConvertUtil.convertToString(map.get("purchasesource"));
		} else {
			contractsource = ConvertUtil.convertToString(map.get("contractnsource"));
		}
		if(StringUtil.isEmpty(contractsource)) {
			result.clear();
			return result;
		}
		if("1".equals(contractsource)) {
			chancecode = ConvertUtil.convertToString(map.get("chancecode"));
			if(StringUtil.isEmpty(chancecode)) {
				result.clear();
				return result;
			}
			result.put("contractsourcecode", chancecode);
		}
		if("2".equals(contractsource)) {
			bidcode = ConvertUtil.convertToString(map.get("bidcode"));
			if(StringUtil.isEmpty(bidcode)) {
				result.clear();
				return result;
			}
			result.put("contractsourcecode", bidcode);
		}
		result.put("contractsource", contractsource);
		return result;
	}


}
